#!/usr/bin/python
"""
    benApp - Simple Computer Game Application for Benjamin

    This file is part of benjap.

    Copyright Graham Jones 2010.

    Benjap is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    banjap is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with benjap (COPYING file).  
    If not, see <http://www.gnu.org/licenses/>.

    $Id$
"""
import os    

# Start pygame
from appBase import appBase
#from icons import icons
import pygame
from pygame.locals import QUIT, KEYDOWN, K_ESCAPE, MOUSEBUTTONDOWN, K_q, K_F4



# Define icon classes
class icons(pygame.sprite.Sprite):
    icon_size = (200,200) # size of icon (pixels)
    iconMargin = 20       # Margin between icons (pixels)
    iconRows = 2          # number of rows of icons
    iconCols = 3          # number of columns of icons
    iconList = []

    def __init__(self, x, y, image,action,*groups):

        pygame.sprite.Sprite.__init__(self, *groups)

        self.x = x
        self.y = y
        self.speed = 0.0
        self.image = pygame.image.load(image)
        self.image = pygame.transform.smoothscale(self.image, 
                                                  icons.icon_size).convert()
        self.action = action

        self.rect = self.image.get_rect(topleft=(int(x), int(self.y)))


    def clickCheck(self,pos):
        """Check if a mouse click is within this icon or not"""
        #if self.rect.collidepoint(pos):
        #    print "Icon %s - clicked" % self.action
        #else:
        #    print "Icon %s - Not clicked" % self.action
        return self.rect.collidepoint(pos)

    def update(self, act_delay):
        """update is only required for animation"""
        pass

    @staticmethod
    def addIcon(iconImage,iconAction,renderGroup):
        print "addIcon: iconImage=%s" % iconImage
        topMargin = 50
        leftMargin = 50
        iconNo = len(icons.iconList)
        row = int(iconNo / icons.iconCols)
        col = iconNo - (row * icons.iconCols)

        print "iconNo=%d, row=%d, col=%d" % (iconNo,row,col)
        
        iconX = topMargin + col * (icons.icon_size[0] + icons.iconMargin)
        iconY = leftMargin + row * (icons.icon_size[1] + icons.iconMargin)

        icons.iconList.append(icons(iconX,iconY,iconImage,iconAction,renderGroup))



class mainApp(appBase):

    def __init__(self):
    
        appBase.__init__(self,None)
        # Load background image
        bg = pygame.image.load('images/stones.jpg')
        self.bg = pygame.transform.smoothscale(bg, self.screen_size).convert()
        
        self.drawBackground()

        self.Sprites = pygame.sprite.RenderUpdates()
        icons.addIcon("images/fireworks.png","fireworks",self.Sprites)
        icons.addIcon("images/comet.png","comet",self.Sprites)
        icons.addIcon("images/star.png","stars",self.Sprites)
        icons.addIcon("images/bounce.png","bounce",self.Sprites)
        icons.addIcon("images/geometry.png","geometry",self.Sprites)
        icons.addIcon("images/photoIcon.png","photo",self.Sprites)

        self.clickedSound = pygame.mixer.Sound('sounds/5381_Jovica_Attack_Zound_01.wav')


    def processEvent(self,event):
        if event.type == MOUSEBUTTONDOWN:
            if event.button == 1:
                for icon in icons.iconList:
                    if (icon.clickCheck(event.pos)):
                        print "icon %s Clicked" % icon.action
                        self.clickedSound.play()
                        if icon.action == "comet":
                            print "runing comet"
                            from cometApp import cometApp
                            sa = cometApp(self)
                            sa.mainLoop()
                            self.drawBackground()
                        if icon.action == "fireworks":
                            print "running fireworks"
                            from fireworkApp import fireworkApp
                            sa = fireworkApp(self)
                            sa.mainLoop()
                            self.drawBackground()
                        if icon.action == "geometry":
                            print "running geometry"
                            from geometryApp import geometryApp
                            sa = geometryApp(self)
                            sa.mainLoop()
                            self.drawBackground()
                        if icon.action == "stars":
                            print "running stars"
                            from starsApp import starsApp
                            sa = starsApp(self)
                            sa.mainLoop()
                            self.drawBackground()
                        if icon.action == "bounce":
                            print "running bounce"
                            from bounceApp import bounceApp
                            sa = bounceApp(self)
                            sa.mainLoop()
                            self.drawBackground()
                        if icon.action == "photo":
                            print "running photo"
                            from photoApp import photoApp
                            sa = photoApp(self)
                            sa.mainLoop()
                            self.drawBackground()


if __name__ == "__main__": 
    ml = mainApp()
    ml.mainLoop()
